<?php if (isset($success_message)) : ?>
    <div class="noty_bar noty_theme_default noty_layout_topCenter noty_success" id="noty_success_1355937644582" style="cursor: pointer; left: 559.5px; display: block;">
        <div class="noty_message"><span class="noty_text">Traslado guardado con éxito</span></div>
    </div>   
<?php endif; ?>
<div class="noty_bar noty_theme_default noty_layout_topCenter noty_error" id="message_error_formAddPurchases" style="cursor: pointer; left: 559.5px;">
	    <div class="noty_message"><span id="contentMessageErrorFormAddPurchase" class="noty_text">Debe elegir algún almacén de origen </span></div>
</div>

    <div class="noty_bar noty_theme_default noty_layout_topCenter noty_error" id="message_error_formAddPurchase" style="cursor: pointer; left: 559.5px;">
    <div class="noty_message"><span id="contentMessageErrorFormAddPurchase" class="noty_text">Debe agregar algún producto</span></div>
    </div>
    

<script type="text/javascript">
    $(document).ready(function() {
   
            $('li[id=type_document_chzn_o_0]').click(function() {
                $.ajax({
                    'url': '<?php echo base_url(); ?>index.php/warehouse_transfer/codetransfer',
                    'success': function(res) {
                        $('input[name=code_transfer]').val('');
                        $('input[name=code_transfer]').val(res);
                        $('#code_transfer').css('background', '#ffffff');

                    }
                });

            })
    })
</script>
<div class="grid_container">

    <form id="formAddPurchase" method="post" action="<?php echo site_url('/warehouse_transfer/submit');?>" class="form_container">
        <div class="grid_12">
            <div class="widget_wrap">
                <div class="widget_top">
                    <span class="h_icon magnifying_glass"></span>
                    <h6>
                       TRASLADO
                    </h6>
                </div>

                <div class="widget_content" style="margin-bottom: 30px">
                    <ul>
                         <li>
                            <div class="form_grid_12">
                                <div class="form_input">
                                    <div class=" form_grid_4 alpha">
                                        <span class="label_head">N° Documento</span>
                                        
                                        <input type="text" name="code_transfer" id="code_transfer" class="noEmpty" value="<?php
                                        if (isset($datesales)) {
                                            foreach ($datesales as $da) {
                                                echo $da->code_transfer;
                                            }
                                        }
                                        ?>" tabindex="2">

                                    </div>
                                    <div class=" form_grid_4">
                                        <span class="label_head">Fecha</span>

                                        <input type="text" name="date" id="numero" value="<?php
                                        if (isset($datesales) && $contandodatos == 0) {
                                            foreach ($datesales as $da) {
                                                echo $da->date;
                                            }
                                        } else {
                                            if (isset($parametro))
                                            {
                                                echo "";
                                            }
                                            else
                                            {
                                                  echo date('d/m/Y');
                                            }
                                          
                                        }
                                        ?>" tabindex="3" <?php
                                        
                                       
                                         if(isset($datesales) && $contandodatos > 0){
                                             echo "";
                                         }else if(isset($datesales) && $contandodatos == 0){
                                             echo " readonly";
                                             
                                         }
                                           
                                        ?>>

                                    </div>
                                    
                                                       
                                    
                                </div>
                                <span class="clear"></span>
                            </div>
                        </li>
                         <table>
	                            <tr>
	                                <td>
                        <li>
                            
                            <div class="form_input">
                            <div class="form_grid_4">
                                        <span class="label_head">Almacén Origen</span>
                                        <?php
                                        $options = array();
                                        $options[0] = "";
                                        foreach ($warehouses as $td) {
                                            $options[$td->id] = $td->name;
                                        }
                                        $extra = 'id="id_warehouse_o" tabindex="3" style="width:340px"  value="0" class="chzn-select full" data-placeholder="Elegir..."';

                                        $buscando_almacen_de_guia = ''; /* variable que contendra el almacen de la guia buscada */
                                        if (isset($datesales) ) {
                                            foreach ($datesales as $da) {
                                                $buscando_almacen_de_guia = $da->id_almacen;
                                            }
                                        }
                                        echo form_dropdown('id_warehouse_o', $options, $buscando_almacen_de_guia, $extra);
                                         ?>
                            </div>
                             </td>
                             <td>
                            <div class="form_grid_4">
                                        <span class="label_head">Almacén Destino</span>
                                        <?php
                                        $options = array();
                                        $options[0] = "";
                                        foreach ($warehouses as $td) {
                                            $options[$td->id] = $td->name;
                                        }
                                        $extra = 'id="id_warehouse_d" tabindex="3" style="width:340px"  value="0" class="chzn-select full" data-placeholder="Elegir..."';

                                        $buscando_almacen_de_guia = ''; /* variable que contendra el almacen de la guia buscada */
                                        if (isset($datesales)) {
                                            foreach ($datesales as $da) {
                                                $buscando_almacen_de_guia = $da->id_almacen;
                                            }
                                        }
                                        echo form_dropdown('id_warehouse_d', $options, $buscando_almacen_de_guia, $extra);
                                         ?>
                            </div>
                            </div> 
                       
                        </li>
                         </td>
                         </tr>
                         <tr>
	                 <td colspan="2">
                        <li>
                            
                                <div class="form_grid_12">
                                <div class="form_input">
                                    <div class="form_grid_12 alpha">
                                        <span class="label_head">Observaciones</span>
                                        <input type="text" name="observations" id="observations" tabindex="5" value="<?php
                                        if (isset($datesales)) {
                                            foreach ($datesales as $da) {
                                                echo $da->observations;
                                            }
                                        } else {

                                        }
                                        ?>">

                                    </div>
                                   

                                    <span class="clear"></span>
                                </div>
                            </div>
                           
                        </li>
                         </td>
	                    <td>	 <div class="form_grid_12">
	                                <div class="form_input">
	                            <button id="addProduct" class="btn_small btn_blue"><span>Agregar Producto</span></button>                                    
	                                    <span class="clear"></span>
	                                </div>
	                            </div>
	                    </td>
	                    </tr>
	                    </table>
                    </ul>
                
                                    <script>
                            $(document).ready(function() {
                                
                                $('#addProduct').click(function() {
                                    var id_warehouse_o = $('#id_warehouse_o').val();
                                    $.ajax({
                                        'url': url + '/warehouse_transfer/products/',
                                        'type': 'POST',
                                        'data': {
                                            'code': '',
                                            'description': '',
                                            'id_warehouse_o': id_warehouse_o
                                        },
                                        'success': function(res) {
                                            $('#add-product-model').html(res);
                                        }
                                    });
                                   
                                  


                                    /*if($("#id_warehouse_o").val() !=0 &&  $("#id_warehouse_d").val() !=0){
                                      
                                                  }
                                                  else
                                                {
                                                  $('div[id=message_error_formAddPurchases]').css('display', 'none');
                                               //alert(" Debe elegir algún almacén de origen  ");

                                                  }

                         */

  if ($('#id_warehouse_o').val() !=0 && $('#id_warehouse_d').val() !=0 ) {




                            } else {

                               
                                $('div[id=message_error_formAddPurchases]').css('display', 'block');



                                    /*if($("#id_warehouse_o").val() !=0 &&  $("#id_warehouse_d").val() !=0){
                                      
                                                  }
                                                  else
                                                {
                                                  $('div[id=message_error_formAddPurchases]').css('display', 'none');
                                               //alert(" Debe elegir algún almacén de origen  ");

                                                  }

                         */}
                                      $('#add-product-model').modal();
                                        return false;
                         


                                    
                                }

                

                                );

                            })
                        </script>

                       
                                    <table class="wtbl_list" border="1">
                        <thead>
                            <tr>
                                <th width="10%">Código</th>
                                <th width="30%">Descripción</th>
                                <th width="10%">Cajas</th>
                                <th width="10%">Empaques</th>
                                <th width="10%">Unidades</th>
                                <th width="10%"></th>
                            </tr>

                        </thead>

                        <tbody id="contentProductList">
                        <tbody id="contentProductList" class="contentProductList">
                        <input type="hidden" name="contador_detalles" id="contador_detalles" value="0" >
                        </tbody>


<?php
if (isset($datesales)) {
    $contandoitem = 0;
   
    foreach ($saledetails as $sa) {                            
        $contandoitem++;       
        ?>
                                <script>
                                    $(document).ready(function() {

                                        $('#contenaumentador').val(<?php echo $contandoitem; ?>);

                                        $('.eliminar_<?php echo $contandoitem; ?>').click(function() {

                                            if (confirm('¿Desea eliminar este producto?')) {

                                                var subtotal_2 = $('input[name=subtotal_<?php echo $contandoitem; ?>_]').val();
                                                var subtotalgeneral = $('input[name=subtotalgeneral]').val();
                                                var total = subtotalgeneral - subtotal_2;
                                                var totaldecimal = (total).toFixed(2);
                                                $('input[name=subtotalgeneral]').val(totaldecimal);


                                                var contandoitem = $('input[name=contandoitem]').val();



                                                $('input[name=contandoitem]').val(contandoitem - 1);

                                                                                               

                                                $('tr.fila_<?php echo $contandoitem; ?>').remove();

                                            } else {

                                            }
                                        })


                                    })
                                </script>  
                                <tr class="fila_<?php echo $contandoitem; ?>">
                                    <td>
                                    <input type="hidden" name="codigo_<?php echo $contandoitem; ?>_" value="<?php echo $sa->code; ?>">
                                    <?php echo $sa->code; ?>
                                    </td>
                                    <td>
                                    <input type="hidden" name="description_<?php echo $contandoitem; ?>_" value="<?php echo $sa->description; ?>">
                                    <?php echo $sa->description; ?>
                                    </td>
                                    <td>
                                    <input type="hidden" name="totalcaja_<?php echo $contandoitem; ?>_" value="<?php echo $sa->box; ?>">
                                    <?php echo $sa->box; ?></td>
                                    <td>
                                    <input type="hidden" name="totalpaguete_<?php echo $contandoitem; ?>_" value="<?php echo $sa->pack; ?>">
                                    <?php echo $sa->pack; ?></td>
                                    <td><input type="hidden" name="totalunidad_<?php echo $contandoitem; ?>_" value="<?php echo $sa->unit; ?>">
                                    <?php echo $sa->unit; ?></td>                                    
                                    <td><a href="#"  class="action-icons c-Delete eliminar_<?php echo $contandoitem; ?>">Delete</a></td>
                                </tr>
                        <?php
                        }
                    } else {
                        
                    }
                    ?>
                    </tbody>
                    </table>
                    <input type="hidden" name="contandoitem" value="<?php
                           if (isset($contandoitem)) {
                               echo $contandoitem;
                           }
                    ?>">
                    <input type="hidden" name="contenaumentador" id="contenaumentador" value="0">
                
                 <script type="text/javascript">
                    $(document).ready(function() { 
                        
                        function checkCode() {
                            $.ajax({
                                'url': url + '/warehouse_transfer/search_code_unique',
                                'type': 'POST',
                                'data': {'code_transfer': $('input[name=code_transfer]').val()},
                                'success': function(res) {
                                 //   alert("alert res: " + res);
                                    if (res == 1) {

                                        alert('El codigo del documento ya fue ingresado anteriormente al sistema.');
                                        $('#code_transfer').css('background', '#ff3333');

                                    } else if (res == 0) {
                                        $('#code_transfer').css('background', '#ffffff');
                                        $('#formAddPurchase').submit();
                                    }
                                }
                            });

                        }

                        $('#code_transfer').focus(function() {
                            $('#code_transfer').css('background', '#ffffff');
                        })
                        $('input[name=date]').focus(function() {
                            $('input[name=date]').css('background', '#ffffff');
                        })


                        function formtrue() {
                            $('formAddPurchase').submit(function() {
                                return true;
                            });
                        }
                        function formfalse() {
                            $('formAddPurchase').submit(function() {
                                return false;
                            });
                        }

                        $('#guardar_transfer').click(function() {
                            if ($('#contenaumentador').val() >= 1) {


                                $('div[id=noty_success_1355937644582]').css('display', 'block');
                                $('div[id=message_error_formAddPurchase]').css('display', 'none');
                                if ($('input[name=code_transfer]').val() !== '') {
                                    
                                    checkCode(); /*funcion ajax submit*/
                                } else {

                                    alert('Coloque codigo del documento');
                                }


                            } else {

                                $('div[id=noty_success_1355937644582]').css('display', 'none');
                                $('div[id=message_error_formAddPurchase]').css('display', 'block');

                                if ($('#code_transfer').val() == '') {
                                    $('#code_transfer').css('background', '#ff3333');
                                }

                                var fechaactual = new Date();


                                if ($('input[name=date]').val() == '') {
                                    $('input[name=date]').css('background', '#ff3333');
                                }





                            }

                        });
                                               
                    })
                    </script>
                    <script type="text/javascript" >

                        function probar(numero) {
                             
                            $('tr.fila_detalle_' + numero + '').remove();
                            
                        }

                        $('button.c-Delete').live('click', function() {
                            if (confirm('¿Desea eliminar este producto?')) {
                            var a = $(this).val();
                            var valor_total_producto = $('input[name=subtotal_' + a + ']').val();
                            var valor_subtotal = $('input[name=subtotalgeneral]').val();

                            var subtotal_catual = (parseFloat(valor_subtotal) - parseFloat(valor_total_producto)).toFixed(2);

                            $('input[name=subtotalgeneral]').val(subtotal_catual);

                            subtotal = parseFloat(subtotal_catual);

                            probar(a);
                            }
                            else
                                {
                                    
                                }
                        });


                        var contando = 0;

                        function contandoitem(dato) {
                            contando = dato;
                            $('#contenaumentador').val(contando);
                        }


                    </script>
                    
                </div>
            </div>
            <br />
            
        <button type="button" id="guardar_transfer" class="btn_small btn_orange"><span>Guardar</span></button>
        <button type="button" class="btn_small btn_orange"><span><a id="buscar_guia" href="<?php echo base_url(); ?>index.php/warehouse_transfer">Cancelar</a></span></button>
        </div>
        
</form>    
</div>
<div id="add-product-model">
</div>
